using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace PersistenceObject.Common
{
    public class TypeHelper
    {
        public static readonly TypeHelper Instance = new TypeHelper();
        private TypeHelper()
        {
            this.bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
        }

        BindingFlags bindingFlags;

        public PropertyInfo GetPropertyInfo(Type type, string propertyName)
        {
            return type.GetProperty(propertyName, this.bindingFlags);
        }

        public object GetPropertyValue(object o, string propertyName)
        {
            return this.GetPropertyInfo(o.GetType(), propertyName).GetValue(o, null);
        }

        public object GetPropertyValue<T>(T t, string propertyName) where T : class
        {
            return this.GetPropertyInfo(t.GetType(), propertyName).GetValue(t, null);
        }

        public void SetPropertyValue<T>(T t, string propertyName, object value) where T : class
        {
            PropertyInfo info = this.GetPropertyInfo(t.GetType(), propertyName);
            //if (value != DBNull.Value)
            //{
            info.SetValue(t, Convert.ChangeType(value, info.PropertyType), null);
            //}
        }
    }
}
